Page({
    data: {
        full_name: "",
        nick_name: "",
        sex: "",
        birthday: "",
        mobile: "",
        city: "",
        province: "",
        cid: "",
        pid: "",
        sexA: ["男", "女"],
        proA:[],
        cityA:[],
        pro:[],
        ci:[],
        proindex:"",
        cityindex:""
    },
    onLoad: function() {
        var self = this;
        wx.showLoading();
        wx.request({
            url: "https://wx.ichelaba.com/htapi/index.php/user/user/index",
            data: {
                user_id: wx.getStorageSync("user_id")
            },
            method: "GET",
            success: function(res) {
                self.setData({
                    full_name: res.data.user_info.full_name,
                    nick_name: res.data.user_info.nick_name,
                    sex: res.data.user_info.sex,
                    birthday: res.data.user_info.birthday,
                    mobile: res.data.user_info.mobile,
                    city: res.data.user_info.city,
                    province: res.data.user_info.province,
                    cid: res.data.user_info.cid,
                    pid: res.data.user_info.pid
                })
                if (res.data.user_info.cid == "") {
                    self.setData({
                        cid: 0
                    })
                }
                if (res.data.user_info.pid == "") {
                    self.setData({
                        pid: 0
                    })
                }
                wx.request({
                  url:"https://wx.ichelaba.com/htapi/index.php/user/address/add",
                  data:{
                    user_id:wx.getStorageSync("user_id")
                  },
                  method:"GET",
                  success:function(res){
                    self.setData({
                      proA:res.data.province_list
                    })
                    var pro=[];
                    var have=false;
                    for(var i=0;i<res.data.province_list.length;i++){
                      if(res.data.province_list[i].district_id==self.data.pid){
                        self.setData({
                          province:res.data.province_list[i].district_name,
                          proindex:i
                        })
                        have=true;
                      }
                      pro.push(res.data.province_list[i].district_name);
                    }
                    self.setData({
                      pro:pro
                    })
                    if(!have){
                      self.setData({
                        pid:res.data.province_list[0].district_id,
                        province:res.data.province_list[0].district_name,
                        proindex:0
                      })
                    }
                    wx.request({
                      url:"https://wx.ichelaba.com/htapi/index.php/user/address/getCity",
                      data:{
                        user_id:wx.getStorageSync("user_id"),
                        pid:self.data.pid
                      },
                      method:"GET",
                      success:function(res){
                        self.setData({
                          cityA:res.data.list
                        })
                        var have=false;
                        var ci=[];
                        for(var i=0;i<res.data.list.length;i++){
                          if(res.data.list[i].district_id==self.data.cid){
                            self.setData({
                              city:res.data.list[i].district_name,
                              cityindex:i
                            })
                            have=true;
                          }
                          ci.push(res.data.list[i].district_name);
                        }
                        self.setData({
                          ci:ci
                        })
                        if(!have){
                          self.setData({
                            cid:res.data.list[0].district_id,
                            city:res.data.list[0].district_name,
                            cityindex:0
                          })
                        }
                        wx.hideLoading();
                      }
                    })
                  }
                })
            }
        })
    },
    bindPickerChange1: function(e) {
        var self = this;
        var json = {
            user_id: wx.getStorageSync("user_id"),
            full_name: self.data.full_name,
            nick_name: self.data.nick_name,
            sex: self.data.sex,
            birthday: e.detail.value,
            mobile: self.data.mobile,
            city: self.data.city,
            province: self.data.province,
            cid: self.data.cid,
            pid: self.data.pid
        };
        wx.showLoading();
        wx.request({
            url: "https://wx.ichelaba.com/htapi/index.php/user/user/saveInfo",
            data: json,
            method: "GET",
            success: function(res) {
                if (res.data.result) {
                    wx.hideLoading();
                    wx.showToast({
                        "title": "修改成功",
                        "mask": true
                    })
                    self.setData({
                        birthday: e.detail.value
                    })
                }
            }
        })
    },
    bindPickerChange2: function(e) {
        var self = this;
        var json = {
            user_id: wx.getStorageSync("user_id"),
            full_name: self.data.full_name,
            nick_name: self.data.nick_name,
            sex: e.detail.value,
            birthday: self.data.birthday,
            mobile: self.data.mobile,
            city: self.data.city,
            province: self.data.province,
            cid: self.data.cid,
            pid: self.data.pid
        };
        wx.showLoading();
        wx.request({
            url: "https://wx.ichelaba.com/htapi/index.php/user/user/saveInfo",
            data: json,
            method: "GET",
            success: function(res) {
                if (res.data.result) {
                    wx.hideLoading();
                    wx.showToast({
                        "title": "修改成功",
                        "mask": true
                    })
                    self.setData({
                        sex: e.detail.value
                    })
                }
            }
        })
    },
    bindChange3:function(e){
      console.log(e);
      var self=this;
      
      if(e.detail.column==0){
        self.setData({
          proindex:e.detail.value
        })
        wx.request({
          url:"https://wx.ichelaba.com/htapi/index.php/user/address/getCity",
          data:{
            pid:self.data.proA[e.detail.value].district_id,
            user_id:wx.getStorageSync("user_id")
          },
          method:"GET",
          success:function(res){
            self.setData({
              cityA:res.data.list
            })
            var ci=[];
            for(var i=0;i<res.data.list.length;i++){
              ci.push(res.data.list[i].district_name);
            }
            self.setData({
              ci:ci
            })
          }
        })
      }else if(e.detail.column==1){
        self.setData({
          cityindex:e.detail.value
        })
      }
    },
    bindChange4:function(e){
      var p=this.data.proA[this.data.proindex].district_id;
      var pn=this.data.proA[this.data.proindex].district_name;
      var c=this.data.cityA[this.data.cityindex].district_id;
      var cn=this.data.cityA[this.data.cityindex].district_name;
      var self=this;
      var json = {
            user_id: wx.getStorageSync("user_id"),
            full_name: self.data.full_name,
            nick_name: self.data.nick_name,
            sex: self.data.sex,
            birthday: self.data.birthday,
            mobile: self.data.mobile,
            city: cn,
            province: pn,
            cid: c,
            pid: p
        };
        wx.showLoading();
        wx.request({
            url: "https://wx.ichelaba.com/htapi/index.php/user/user/saveInfo",
            data: json,
            method: "GET",
            success: function(res) {
                if (res.data.result) {
                    wx.hideLoading();
                    wx.showToast({
                        "title": "修改成功",
                        "mask": true
                    })
                    self.setData({
                        pid: p,
                        cid: c,
                        province: pn,
                        city: cn
                    })
                }
            }
        })
    }
})